class Entry < ActiveRecord::Base

  belongs_to :runner

  validates_numericality_of :distance, :message => ': Distance ran must be a number'
  validates_numericality_of :sessions, :message => ': Sessions ran must be a number'
  validate :Distance_must_be_greater_then_0




  after_save :update_caches_for_runner
  after_destroy :update_caches_for_runner
  after_update :update_caches_for_runner
  
  def info
    "#{sessions} sessions / #{distance} miles"
  end

protected 
  def Distance_must_be_greater_then_0
    errors.add(:distance, 'Please enter the number of total laps to be greater then 0') if distance <= 0 
  end 

  private

    def update_caches_for_runner
      runner.update_caches!
    end

end
